@extends('app')

@section('content')
    <div class="layui-card" style="margin-bottom: 0;">
        @include('layouts.breadcrumb')

        <div class="layui-tab-content">
            <form class="layui-form">
                <blockquote class="layui-elem-quote quoteBox" >
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select class="" name="cates_id">
                                <option value="">选择分类</option>
                                @foreach($cates as $key=>$item)
                                    <option value="{{$key}}" @if($cates_id==$key) selected @endif>{{$item}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select class="" name="tab">
                                <option value="">选择分类</option>
                                @foreach($tabs as $key=>$item)
                                    <option value="{{$key}}" @if($tab==$key) selected @endif>{{$item}}</option>
                                @endforeach
                            </select>
                        </div>

                    </div>
                    <a class="layui-btn search_btn" data-type="reload" lay-submit lay-filter="search" style="margin-left: 3px"><i class="layui-icon layui-icon-search"></i>查询</a>
                </blockquote>
            </form>
            <div class="layui-tab-item layui-show">
                <table class="layui-hide" id="model" lay-filter="model"></table>
            </div>
        </div>
    </div>

    {{--表头操作--}}
    <script type="text/html" id="upload">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="edit">新增</button>
            <button class="layui-btn layui-btn-sm" id="sort" >保存排序</button>
        </div>
    </script>

    {{--表列操作--}}
    <script type="text/html" id="operate">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="comment">评论</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
@endsection

@section('js')
    <script>
        layui.config({
            version:false
        }).use('table', function(){
            var table = layui.table;
            var form = layui.form;
            var $   = layui.$;


            table.render({
                elem: '#model',
                url:'/article/index',
                cellMinWidth: 95, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                height: 'full-150',
                method : 'POST',
                page: true, //开启分页
                defaultToolbar: ['filter', 'print', 'exports'],
                toolbar:'#upload',
                limit:20,
                loading:true,
                rowStyle: function (index, item) {//index第几行，row当前行的值
                    return 'data-id="'+item.id+'" ';
                },
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', title: 'ID',align:'center'},
                    {field:'cates',width:'180',title: '分类',templet:function(item){return item.cates.name;}},
                    {field:'title',width:'180',title: '标题'},
                    {field:'tabs',title: '标签',templet:function(item){return item.tabs.name;}},
                    {field:'user_id',width:'180',title: '发表用户',templet:function(item){return item.user.username;}},
                    {field:'watch_count',width:'100',title: '浏览量'},
                    {field:'keep_count',width:'100',title: '收藏量'},
                    {field:'keep_count',width:'100',title: '评论量',templet:function (item) {
                                return '<a class="layadmin-user-jump-change layadmin-link" onclick="newTab(\''+item.title+'\',\'/comment/index?article='+item.id+'\')">'+item.comment_count+'</a>'
                        }},
                    {field:'content',width:'300',title: '内容'},
                    {field:'updated_at',width:'180',title: '更新时间',templet:function(item){return dateTime(item.updated_at)}},
                    {title: '操作',width:200, align:'center', fixed: 'right', toolbar: '#operate'}
                ]]
            });

            //表头操作
            table.on('toolbar(model)', function(obj){
                switch(obj.event){
                    case 'edit':
                            layer.full(layer.open({
                                type: 2,
                                title: '新增文章',
                                shade: false,
                                content:'/article/edit',
                                maxmin: true
                            }))
                        // newWindow('新增文章','/article/edit',500,500,true);
                        break;
                };
            });

            //列操作
            table.on('tool(model)',function (obj) {
                if(obj.event === 'del'){//删除行
                    del(obj);
                }else if(obj.event==="edit"){
                    var data = obj.data;
                    layer.full(layer.open({
                        type: 2,
                        title: '新增文章',
                        shade: false,
                        content:'/article/edit?id='+data.id,
                        maxmin: true
                    }))
                    // newWindow('编辑','/article/edit?id='+data.id,500,500,true);
                }else if(obj.event==="comment"){
                    var data = obj.data;
                    layer.full(layer.open({
                        type: 2,
                        title: '新增文章',
                        shade: false,
                        content:'/article/comment?id='+data.id,
                        maxmin: true
                    }))
                    // newWindow('评论','/article/comment?id='+data.id,500,500,true);
                }
            });

            function del(data) {

                layer.confirm("你确定删除这篇文章么?", {icon: 3, title:'提示'},
                    function(index){//确定回调
                        var id = data.data.id;
                        console.info(id);
                        $.ajax({
                            url:'/article/del',
                            data:{id:id},
                            success:function(data){
                                layer.alert(data.msg,{icon: 1},function () {
                                    location.reload('model');
                                    parent.layer.closeAll();
                                });
                            }
                        })
                    },function (index) {//取消回调
                        layer.close(index);
                    }
                );
            }

        })
    </script>
@endsection